#include <Ewl.h>
#include <stdio.h>
#include <stdlib.h>
#include "eedit_gui.h"

#define HDEF 640
#define WDEF 320

#ifdef DEBUG
#define PRINTFUNC(x) printf("DEBUG: %s\n",x);
#else
#define PRINTFUNC(x)
#endif

Ewl_Widget *win = NULL;

void destroy_real(Ewl_Widget *w, void *event, void *data){
    PRINTFUNC(__PRETTY_FUNCTION__);
    ewl_widget_destroy(w);
    ewl_main_quit();
}

void destroy_cb(Ewl_Widget *w, void *event, void *data){
    PRINTFUNC(__PRETTY_FUNCTION__);
    run_dialog(w,event,data);
}

void text_update_cb(Ewl_Widget *w, void *event, void *data){
    PRINTFUNC(__PRETTY_FUNCTION__);
    char *s = NULL;
    Ewl_Widget *label = NULL;
    char buf[BUFSIZ];

    s = ewl_text_text_get(EWL_TEXT(w));
    label = (Ewl_Widget *)data;

    snprintf(buf, BUFSIZ, "Hello %s", s);
    ewl_text_text_append(EWL_TEXT(label), buf);

    free(s);
    return;
}

static void dialog_delete_cb(Ewl_Widget *w, void *ev, void *data){
    PRINTFUNC(__PRETTY_FUNCTION__);
    ewl_widget_destroy(EWL_WIDGET(data));
}

static void dialog_response_cb(Ewl_Widget *w, void *ev, void *data){
    PRINTFUNC(__PRETTY_FUNCTION__);
    Ewl_Stock_Type response;
    Ewl_Widget *o;

    o = ewl_widget_name_find("Quit Dialog");
    response = ewl_stock_type_get(EWL_STOCK( w));

    if (response == EWL_STOCK_OK){
        ewl_widget_destroy(EWL_WIDGET(data));
        destroy_real(win,ev,data);
    }else{
        ewl_widget_destroy(EWL_WIDGET(data));
    }
}

static void run_dialog(Ewl_Widget *w, void *ev __UNUSED__, void *data __UNUSED__){
    PRINTFUNC(__PRETTY_FUNCTION__);
    Ewl_Widget *o, *dialog, *hbox;
#if 0
    Ewl_Window *win;
    win = EWL_WINDOW(ewl_embed_widget_find(w));
#endif

    dialog = ewl_dialog_new();
    ewl_window_title_set(EWL_WINDOW(dialog), "Quit Dialog");
    ewl_window_name_set(EWL_WINDOW(dialog), "QuitDialog");
    ewl_window_class_set(EWL_WINDOW(dialog), "EFL Test Application");
    ewl_window_transient_for(EWL_WINDOW(dialog),EWL_WINDOW(win));
    ewl_callback_append(dialog, EWL_CALLBACK_DELETE_WINDOW, dialog_delete_cb, NULL);
    ewl_widget_show(dialog);
    ewl_dialog_active_area_set(EWL_DIALOG(dialog), EWL_POSITION_TOP);
    
    hbox = ewl_hbox_new();
    ewl_container_child_append(EWL_CONTAINER(dialog), hbox);
    ewl_widget_show(hbox);

    o = ewl_image_new();
    ewl_image_file_set(EWL_IMAGE(o),
            ewl_icon_theme_icon_path_get(EWL_ICON_DIALOG_INFORMATION, 46),
            EWL_ICON_DIALOG_INFORMATION);
    ewl_container_child_append(EWL_CONTAINER(hbox), o);
    ewl_object_padding_set(EWL_OBJECT(o), 20,20,20,20);
    ewl_widget_show(o);

    o = ewl_text_new();
    ewl_object_fill_policy_set(EWL_OBJECT(o), EWL_FLAG_FILL_NONE);
    ewl_object_alignment_set(EWL_OBJECT(o), EWL_FLAG_ALIGN_CENTER);
    ewl_container_child_append(EWL_CONTAINER(hbox), o);
    ewl_text_text_set(EWL_TEXT(o), "Are you sure you want to exit?");
    ewl_widget_show(o);

    ewl_dialog_active_area_set(EWL_DIALOG(dialog), EWL_POSITION_BOTTOM);

    o = ewl_button_new();
    ewl_stock_type_set(EWL_STOCK(o), EWL_STOCK_OK);
    ewl_container_child_append(EWL_CONTAINER(dialog), o);
    ewl_callback_append(o, EWL_CALLBACK_CLICKED, dialog_response_cb, dialog);
    ewl_widget_show(o);

    o = ewl_button_new();
    ewl_stock_type_set(EWL_STOCK(o), EWL_STOCK_CANCEL);
    ewl_container_child_append(EWL_CONTAINER(dialog), o);
    ewl_callback_append(o, EWL_CALLBACK_CLICKED, dialog_response_cb, dialog);
    ewl_widget_show(o);

    ewl_dialog_active_area_set(EWL_DIALOG(dialog), EWL_POSITION_TOP);
    ewl_widget_show(dialog);
}

int main(int argc, char **argv){
    PRINTFUNC(__PRETTY_FUNCTION__);
/*  Ewl_Widget *win moved to be global */
    Ewl_Widget *box = NULL;
    Ewl_Widget *label = NULL;
    Ewl_Widget *o = NULL;
    Ewl_Widget *h_menubar = NULL;
    Ewl_Widget *scrollpane = NULL;
    Ewl_Widget *foo = NULL;

    /*Init the Library*/
    if(!ewl_init(&argc, argv)){
        printf("Unable to init ewl\n");
        return 1;
    }
    
    /* "Create" new window object and assign it to our widget */
    win = ewl_window_new();
    /* Window Properties */
    ewl_window_title_set(EWL_WINDOW(win), "eedit");
    ewl_window_name_set(EWL_WINDOW(win), "eedit_main_window");
    ewl_window_class_set(EWL_WINDOW(win), "EWLWindow");
    ewl_object_fill_policy_set(EWL_OBJECT(win), EWL_FLAG_FILL_ALL);
    ewl_object_size_request(EWL_OBJECT(win), HDEF, WDEF);
    /* Program stuff */
    ewl_callback_append(win, EWL_CALLBACK_DELETE_WINDOW, run_dialog, NULL);
    ewl_widget_show(win);
    

    /* Create the container */
    box = ewl_vbox_new();
    /* Add the box to the window */
    ewl_container_child_append(EWL_CONTAINER(win), box);
    ewl_object_fill_policy_set(EWL_OBJECT(box), EWL_FLAG_FILL_ALL);
    ewl_widget_show(box);
    
    /* Build the Menu bar */
    h_menubar = ewl_hmenubar_new();
    {
        int i;
        Ewl_Widget *item;
        Ewl_Widget *foo;
        Ewl_Widget *sub;
        Ewl_Widget *sep;
/* File Menu */        
        item = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(item), "File");
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_widget_show(item);
            
        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "New  CTRL + n");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/filenew.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);
        
        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Open  CTRL + o");
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);
        
        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Save  CTRL + s");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/filesave.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);
    
        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Close  CTRL + Q");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/fileclose.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);
        
        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Quit Application");
        ewl_callback_append(foo, EWL_CALLBACK_CLICKED, run_dialog, NULL);
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/exit.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);
#if 1 
/* Seperator */        
        sep = ewl_vseparator_new();
        ewl_container_child_append(EWL_CONTAINER(h_menubar), sep);
        ewl_widget_show(sep);
#endif
/* Options Menu */
        item = ewl_menu_new();
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_button_label_set(EWL_BUTTON(item), "Options");
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_widget_show(item);
            
        sub = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(sub), "Syntax Highlighting");
        ewl_container_child_append(EWL_CONTAINER(item), sub);
        ewl_widget_show(sub);
        
        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "C / C++");
        ewl_container_child_append(EWL_CONTAINER(sub), foo);
        ewl_widget_show(foo);
        
        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "Java");
        ewl_container_child_append(EWL_CONTAINER(sub), foo);
        ewl_widget_show(foo);

/* Seperator */        
        sep = ewl_vseparator_new();
        ewl_container_child_append(EWL_CONTAINER(h_menubar), sep);
        ewl_widget_show(sep);
      
        item = ewl_menu_new();
        ewl_button_label_set(EWL_BUTTON(item), "Help");
        ewl_button_image_set(EWL_BUTTON(item),EAPPS_DATA_DIR "/pixmaps/edit_add.png",NULL);
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_widget_show(item);

        foo = ewl_menu_item_new();
        ewl_button_label_set(EWL_BUTTON(foo), "About");
        ewl_button_image_set(EWL_BUTTON(foo),EAPPS_DATA_DIR "/pixmaps/help.png",NULL);
        ewl_container_child_append(EWL_CONTAINER(item), foo);
        ewl_widget_show(foo);
        
        sep = ewl_vseparator_new();
        ewl_container_child_append(EWL_CONTAINER(h_menubar), sep);
        ewl_widget_show(sep);
      
        item = ewl_spacer_new();
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_ALL);
        ewl_container_child_append(EWL_CONTAINER(h_menubar), item);
        ewl_widget_show(item);

/*Show Menu*/        
        ewl_container_child_append(EWL_CONTAINER(box), h_menubar);
        ewl_object_fill_policy_set(EWL_OBJECT(item), EWL_FLAG_FILL_SHRINK);
        ewl_object_alignment_set(EWL_OBJECT(h_menubar), EWL_FLAG_ALIGN_LEFT);
        ewl_widget_show(h_menubar);
    }

    /* Create a label */
    label = ewl_text_new();
    /* Add the label to the box */
    ewl_container_child_append(EWL_CONTAINER(box), label);
    ewl_object_fill_policy_set(EWL_OBJECT(label), EWL_FLAG_FILL_NONE);
    ewl_text_styles_set(EWL_TEXT(label), EWL_TEXT_STYLE_DOUBLE_UNDERLINE
                                    | EWL_TEXT_STYLE_OUTLINE
                                                            | EWL_TEXT_STYLE_SOFT_SHADOW);
    ewl_text_double_underline_color_set(EWL_TEXT(label), 50, 50, 50, 255);
    ewl_text_shadow_color_set(EWL_TEXT(label), 128, 128, 128, 128);
    ewl_text_outline_color_set(EWL_TEXT(label), 200, 200, 200, 200);
    
    ewl_text_text_append(EWL_TEXT(label), "Enlightened Editing");
    ewl_widget_show(label);
    
    scrollpane = ewl_scrollpane_new();
#if 0
    ewl_scrollpane_hscrollbar_flag_set(EWL_SCROLLPANE(scrollpane),
            EWL_SCROLLPANE_FLAG_ALWAYS_HIDDEN);
#endif
    ewl_container_child_append(EWL_CONTAINER(box), scrollpane);
    ewl_widget_name_set(scrollpane, "scrollpane");
    ewl_widget_show(scrollpane);
    
    /* Create Entry*/
    o = ewl_entry_new();
    /* Add to the box */
    ewl_container_child_append(EWL_CONTAINER(scrollpane), o);
    ewl_object_fill_policy_set(EWL_OBJECT(o), EWL_FLAG_FILL_NONE);
    ewl_widget_name_set(o,"entry");
    ewl_entry_multiline_set(EWL_ENTRY(o), TRUE);
    ewl_text_wrap_set(EWL_TEXT(o), EWL_TEXT_WRAP_WORD);
    ewl_text_font_size_set(EWL_TEXT(o), 12);

    ewl_text_underline_color_set(EWL_TEXT(o), 0, 0, 0, 255);
    ewl_text_double_underline_color_set(EWL_TEXT(o), 50, 50, 50, 255);
    ewl_text_shadow_color_set(EWL_TEXT(o), 128, 128, 128, 128);
    ewl_text_outline_color_set(EWL_TEXT(o), 200, 200, 200, 200);
    ewl_text_strikethrough_color_set(EWL_TEXT(o), 0, 0, 0, 255);
    ewl_text_glow_color_set(EWL_TEXT(o), 0, 255, 0, 128);
    ewl_text_selectable_set(EWL_TEXT(o), 1);

    foo = ewl_statusbar_new();
    ewl_container_child_append(EWL_CONTAINER(box), foo);
    ewl_statusbar_push(EWL_STATUSBAR(foo), "Line: 1  Cursor: 1  Last: 0");
    ewl_widget_name_set(foo,"statusbar");
    ewl_widget_show(foo);

    ewl_widget_show(o); 
#ifdef SHOW_APPEND_BUG    
    ewl_text_text_append(EWL_TEXT(o), txt);
#else
    ewl_text_text_set(EWL_TEXT(o), txt);
#endif


    ewl_main();
    return 0;
}
